#include "FiveWin.ch"

//----------------------------------------------------------------------------//

function PrgNew( oMruPrgs )

   PrgOpen( cNewFileName( "programs\NONAM", "prg" ), oMruPrgs )

return nil

//----------------------------------------------------------------------------//

function PrgOpen( cPrgName, oMruPrgs )

   local oWndEdit, oBar, oBtn, oGet, oCrsPen, oFont, oRow, oCol
   local cText, oTxtEdit
   local cDlgCode := "   local oDlg" + CRLF + CRLF + ;
               "   DEFINE DIALOG oDlg RESOURCE '...'" + CRLF + CRLF + ;
               "   ACTIVATE DIALOG oDlg CENTERED" + CRLF

   DEFAULT cPrgName := cGetFile( "Clipper Program (*.prg) | *.prg",;
                                 "Select a PRG", 1, "Programs" )
   if Empty( cPrgName )
      return nil
   else
      if File( cPrgName )
         cText = MemoRead( cPrgName )
      else
         cText  = '#include "FiveWin.ch"' + CRLF + CRLF
         cText += "//----------------------------------------------" + ;
                  "------------------------------//" + CRLF + CRLF
         cText += "function Main()" + CRLF + CRLF + CRLF + CRLF
         cText += "return nil" + CRLF + CRLF
         cText += "//----------------------------------------------" + ;
                  "------------------------------//" + CRLF
      endif
   endif

   DEFINE WINDOW oWndEdit ;
      TITLE "Program: " + cFileNoExt( cFileNoPath( cPrgName ) ) ;
      ICON "Write"

   DEFINE BUTTONBAR oBar _3D SIZE 33, 33 OF oWndEdit

   DEFINE BUTTON RESOURCE "Save" OF oBar ;
      TOOLTIP "Save" ;
      MESSAGE "Save to disk" ;
      ACTION ( oGet:Save(), MemoWrit( cPrgName, cText ), Tone( 900, 1 ) )

   DEFINE BUTTON RESOURCE "Compile" GROUP OF oBar ;
      TOOLTIP "Compile" ;
      MESSAGE "Compile this program" ;
      ACTION ( oGet:Save(),;
               MemoWrit( cPrgName, cText ),;
               WinExec( "..\tools\wClipper.exe " + cFileNoExt( cPrgName ) ) )

   DEFINE BUTTON RESOURCE "Link" OF oBar ;
      TOOLTIP "Link" ;
      MESSAGE "Link this program" ;
      ACTION WinExec( "link.pif " + cFileNoExt( cPrgName ) )

   DEFINE BUTTON RESOURCE "Exec"    OF oBar ;
      TOOLTIP "Run" ;
      MESSAGE "Execute this program" ;
      ACTION WinExec( cFileNoExt( cPrgName ) )

   DEFINE BUTTON OF oBar GROUP RESOURCE "Comment" TOOLTIP "Insert comment" ;
      ACTION oGet:Paste( "//" + Replicate( "-", 76 ) + "//" + CRLF + CRLF ),;
             oGet:SetFocus()

   DEFINE BUTTON OF oBar RESOURCE "Function" TOOLTIP "function ... return" ;
      ACTION oGet:Paste( "function " + CRLF + CRLF + CRLF + CRLF + ;
                         "return nil" + CRLF + CRLF ),;
             oGet:SetFocus()

   DEFINE BUTTON OF oBar RESOURCE "IfEndif" TOOLTIP "if ... endif" ;
      ACTION oGet:Paste( "   if " + CRLF + CRLF + "   endif" + CRLF + CRLF ),;
             oGet:SetFocus()

   DEFINE BUTTON OF oBar RESOURCE "DoCase" TOOLTIP "do case ... endcase" ;
      ACTION oGet:Paste( "   do case " + CRLF + CRLF + ;
                         "      case" + CRLF + CRLF + ;
                         "      case" + CRLF + CRLF + ;
                         "      otherwise" + CRLF + CRLF + ;
                         "   endcase" + CRLF + CRLF ),;
             oGet:SetFocus()

   DEFINE BUTTON OF oBar RESOURCE "DoWhile" TOOLTIP "while ... end" ;
      ACTION oGet:Paste( "   while " + CRLF + CRLF + ;
                         "   end" + CRLF + CRLF ),;
             oGet:SetFocus()

   DEFINE BUTTON oBtn OF oBar GROUP RESOURCE "FuncList" ;
      TOOLTIP "Functions List" ACTION FuncList( oBtn, oGet )

   DEFINE BUTTON OF oBar GROUP RESOURCE "Dialog" ;
      TOOLTIP "Dialog" ;
      ACTION ( oGet:Paste( cDlgCode ), oGet:SetFocus() )

   SET MESSAGE OF oWndEdit TO "" NOINSET

   DEFINE MSGITEM oCol OF oWndEdit:oMsgBar PROMPT "Col: 0" SIZE 70

   DEFINE MSGITEM oRow OF oWndEdit:oMsgBar ;
      PROMPT "Row: 0" SIZE 70 ACTION GoToLine( oGet, oRow )

   DEFINE FONT oFont NAME "Courier New" SIZE 0, -13

   oTxtEdit = TTxtEdit():New( 0, 0, 100, 100, oWndEdit )
   oTxtEdit:SetText( cText )
   oWndEdit:oClient = oTxtEdit

   ACTIVATE WINDOW oWndEdit ;
      VALID ( oMruPrgs:Save( cPrgName ), .t. )

return nil

//----------------------------------------------------------------------------//

function FuncList( oBtn, oGet )

   local oDlg, oLbx
   local cText  := oGet:GetText()
   local n, nLines := oGet:GetLineCount(), cLine
   local aFuncs := {}, aLines := {}

   if At( "FUNCTION", Upper( cText ) ) == 0
      MsgAlert( "No functions declared in this PRG" )
      return nil
   endif

   CursorWait()

   for n = 1 to nLines
       cLine = oGet:GetLine( n )
       if SubStr( Upper( LTrim( cLine ) ), 1, 8 ) == "FUNCTION"
          AAdd( aFuncs, cLine )
          AAdd( aLines, n )
       endif
       SysRefresh()
   next

   if Len( aFuncs ) == 0
      AAdd( aFuncs, "Not defined" )
   endif

   cLine = aFuncs[ 1 ]

   DEFINE DIALOG oDlg RESOURCE "FuncList"

   REDEFINE LISTBOX oLbx VAR cLine ITEMS aFuncs ID 110 OF oDlg ;
      ON DBLCLICK ( oGet:GoTo( aLines[ oLbx:GetPos() ] ), oDlg:End() )

   REDEFINE BUTTON ID ID_OK OF oDlg ;
      ACTION ( oGet:GoTo( aLines[ oLbx:GetPos() ] ), oDlg:End() )

   ACTIVATE DIALOG oDlg CENTER

return nil

//----------------------------------------------------------------------------//

static function GotoLine( oGet, oRow )

   local oDlg
   local nLine := oGet:GetRow()

   DEFINE DIALOG oDlg RESOURCE "GotoLine"

   REDEFINE GET nLine ID 110 OF oDlg

   REDEFINE BUTTON ID 120 OF oDlg ;
      ACTION ( oGet:GoTo( nLine ),;
               oRow:SetText( "Row: " + LTrim( Str( nLine ) ) ),;
               oDlg:End() )

   ACTIVATE DIALOG oDlg CENTERED

return nil

//----------------------------------------------------------------------------//